package ru.usu.gv.gui.component;

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 * @author spupyrev
 */
public class GVIntegerField extends JPanel
{
    private JTextField field;
    private int value;

    public GVIntegerField(String label, int initValue, boolean enabled)
    {
        value = initValue;
        field = new JTextField(String.valueOf(initValue));
        field.setEnabled(enabled);
        field.setPreferredSize(new Dimension(50, 20));

        JLabel jLabel = new JLabel(label + "  ");
        setLayout(new BorderLayout());
        add(BorderLayout.WEST, jLabel);
        add(BorderLayout.CENTER, field);
    }

    public int getValue()
    {
        String text = field.getText();
        try
        {
            value = Integer.parseInt(text);
        }
        catch (NumberFormatException e)
        {
            field.setText(String.valueOf(value));
        }

        return value;
    }
}
